package com.zgjkhis.modules.charges.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.charges.domain.PrescriptionOrder;
import com.zgjkhis.modules.charges.service.dto.OrderChargeDto;
import com.zgjkhis.modules.charges.service.dto.PrescriptionOrderDto;
import com.zgjkhis.modules.charges.service.dto.PrescriptionQueryCriteria;
import com.zgjkhis.modules.outpatient.dto.LocalOrderDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yxl
 * @Description
 * @Date 2021/7/13 16:04
 **/
@Repository
public interface PrescriptionOrderMapper extends CoreMapper<PrescriptionOrder> {

    @Select("<script>\nselect " +
            "o.id, " +
            "o.order_no, " +
            "o.patient_name, " +
            "o.patient_sex,  " +
            "o.patient_age,  " +
            "o.order_time, " +
            "o.total_fee, " +
            "o.doc_name, " +
            "o.pay_status, " +
            "o.delivery_type, " +
            "o.remark, " +
            "o.pay_time, " +
            "o.create_time, " +
            "o.order_condition, " +
            "o.api_order_no, " +
            "info.pst_type, " +
            "info.dosage_form, " +
            "info.prescription_type " +
            "from (select * from his_prescription_order oo where order_type=2 and oo.pharmacy_id = #{order.pharmacyId}" +
            "<if test=\"order.payStatus != null\"> " +
            " and oo.pay_status = #{order.payStatus} " +
            "</if> " +
            "<choose>"+
            "<when test=\"order.orderCondition > 0 and order.orderCondition &lt; 4\"> " +
            " and oo.order_condition = #{order.orderCondition} " +
            " and oo.pay_status != 4 " +
            "</when> " +
            "<when test=\"order.orderCondition != null\"> " +
            " and oo.order_condition = #{order.orderCondition} " +
            "</when> " +
            "</choose>"+

            "<if test=\"order.patientName != null\"> " +
            " and oo.patient_name LIKE CONCAT('%',#{order.patientName},'%') " +
            "</if> " +
            "<if test=\"order.docName != null\"> " +
            " and oo.doc_name LIKE CONCAT('%',#{order.docName},'%') " +
            "</if> " +
            "<if test=\"order.orderTime != null and order.orderTime.size() > 0 \">\n" +
            "and oo.create_time  &gt; #{order.orderTime[0]}\n" +
            "and oo.create_time  &lt;  str_to_date(DATE_ADD(#{order.orderTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>" +
            "<if test=\"order.payTime != null and order.payTime.size() > 0 \">\n" +
            "and oo.pay_time  &gt; #{order.payTime[0]}\n" +
            "and oo.pay_time  &lt;  str_to_date(DATE_ADD(#{order.payTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>"+
            ") o " +
            "left join his_prescription_info info on o.order_no = info.order_no " +
            "<if test=\"order.pstType != null\"> " +
            "where info.pst_type = #{order.pstType} " +
            "</if> " +
            "order by o.create_time desc" +
            "\n</script>")
    List<PrescriptionOrderDto> findAllByPageable(@Param("order") PrescriptionQueryCriteria criteria);


    @Select("<script>\nselect distinct " +
            "o.id, " +
            "o.order_no, " +
            "o.patient_name, " +
            "o.patient_sex,  " +
            "o.patient_age,  " +
            "o.total_fee, " +
            "o.doc_name, " +
            "o.pay_status, " +
            "o.order_condition, " +
            "o.delivery_type, " +
            "o.remark, " +
            "o.pay_time, " +
            "o.order_time, " +
            "o.create_time, " +
            "o.diff_fee, " +
            "o.drug_discount, " +
            "o.all_discount, " +
            "o.other_fee, " +
            "o.pay_way, " +
            "o.actual_charge, " +
            "o.api_order_no, " +
            "o.order_type, " +
            "o.source, " +
            "o.cirulation_cost, " +
            "o.cir_order_no, " +
            "o.cir_info_no, " +
            "o.adjust_time, " +
            "o.next_time, " +
            "o.next_remark, " +
            "o.next_flag, " +
            "o.refund_flag, " +
            "o.old_order_no, " +
            "o.reissue_reason, " +
            "o.refund_type, " +
            "o.source_store_name " +
            "from his_prescription_order o "+
            " left join his_account a on o.patient_id=a.id "+
            "where o.pharmacy_id = #{order.pharmacyId}" +
            "<if test=\"order.payStatus != null and order.payStatus!=34\"> " +
            " and o.pay_status = #{order.payStatus} " +
            "</if> " +
            "<if test=\"order.payStatus != null and order.payStatus==34\"> " +
            " and o.pay_status in(3,4) " +
            "</if> " +
            "<if test=\"order.printStatus != null and order.printStatus==1\"> " +
            " and o.order_no in(select order_no from his_prescription_order_print) " +
            "</if> " +
            "<if test=\"order.printStatus != null and order.printStatus==2\"> " +
            " and o.pay_status !=4  and  o.order_no not in(select order_no from his_prescription_order_print) " +
            "</if> " +
            "<if test=\"order.isFlag != null and order.isFlag==1 \"> " +
            " and o.order_condition in(3,4,5) and o.pay_status in(1,3) " +
            "</if> " +
            "<if test=\"order.orderType != null and order.orderType !=''\"> " +
            " and o.order_type = #{order.orderType} " +
            "</if> " +
            "<if test=\"order.orderCondition != null\"> " +
            " and o.order_condition = #{order.orderCondition} " +
            "</if> " +
            "<if test=\"order.deliveryType != null and order.deliveryType !=''\"> " +
            " and o.delivery_type = #{order.deliveryType} " +
            "</if> " +
            "<if test=\"order.patientName != null and order.patientName !=''\"> " +
            " and (o.patient_name LIKE CONCAT('%',#{order.patientName},'%') or a.phone=#{order.patientName}) " +
            "</if> " +
            "<if test=\"order.orderNos != null and order.orderNos.size() > 0\"> " +
            " and o.order_no in <foreach item=\"item\" index=\"index\" collection=\"order.orderNos\" open=\"(\" separator=\",\" close=\")\"> #{item} </foreach>"+
            "</if> " +
            "<if test=\"order.orderNo != null and order.orderNo !=''\"> " +
            " and (o.order_no=#{order.orderNo} or o.api_order_no=#{order.orderNo}) " +
            "</if> " +
            "<if test=\"order.docName != null and order.docName!=''\"> " +
            " and o.doc_name LIKE CONCAT('%',#{order.docName},'%') " +
            "</if> " +
            "<if test=\"order.docId != null and order.docId!=''\"> " +
            " and o.doc_id =#{order.docId} " +
            "</if> " +
            "<if test=\"order.orderTime != null and order.orderTime.size() > 0 \">\n" +
            "and o.create_time  &gt; #{order.orderTime[0]}\n" +
            "and o.create_time  &lt;  str_to_date(DATE_ADD(#{order.orderTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>" +
            "<if test=\"order.payTime != null and order.payTime.size() > 0 \">\n" +
            "and o.pay_time  &gt; #{order.payTime[0]}\n" +
            "and o.pay_time  &lt; str_to_date(DATE_ADD(#{order.payTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>"+
            "<if test=\"order.adjustTime != null and order.adjustTime.size() > 0 \">\n" +
            "and ((o.adjust_time  <![CDATA[ >= ]]> #{order.adjustTime[0]}\n" +
            "and o.adjust_time  &lt; str_to_date(DATE_ADD(#{order.adjustTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')) " +
            "or (" +
            "o.next_time  <![CDATA[ >= ]]> #{order.adjustTime[0]}\n" +
            "and o.next_time  &lt; str_to_date(DATE_ADD(#{order.adjustTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "))\n" +
            "</if>"+
            "order by o.next_time desc,o.pay_time desc,o.create_time desc" +
            "\n</script>")
    List<LocalOrderDto> findAllByPageableForLocal(@Param("order") PrescriptionQueryCriteria criteria);


    @Select("<script>\nselect distinct " +
            "o.id, " +
            "o.order_no, " +
            "o.patient_name, " +
            "o.patient_sex,  " +
            "o.patient_age,  " +
            "o.total_fee, " +
            "o.doc_name, " +
            "o.pay_status, " +
            "o.order_condition, " +
            "o.delivery_type, " +
            "o.remark, " +
            "o.pay_time, " +
            "o.order_time, " +
            "o.create_time, " +
            "o.diff_fee, " +
            "o.drug_discount, " +
            "o.all_discount, " +
            "o.other_fee, " +
            "o.pay_way, " +
            "o.actual_charge, " +
            "o.api_order_no, " +
            "o.order_type, " +
            "o.source, " +
            "o.cirulation_cost, " +
            "o.cir_order_no, " +
            "o.cir_info_no, " +
            "o.adjust_time, " +
            "o.next_time, " +
            "o.next_remark, " +
            "o.next_flag, " +
            "o.refund_flag, " +
            "o.old_order_no, " +
            "o.reissue_reason, " +
            "o.refund_type, " +
            "o.source_store_name " +
            "from his_prescription_order o "+
            " left join his_account a on o.patient_id=a.id "+
            " left join his_prescription_info info on info.order_no=o.order_no "+
            "where o.pharmacy_id = #{order.pharmacyId}" +
            "<if test=\"order.payStatus != null and order.payStatus!=34\"> " +
            " and o.pay_status = #{order.payStatus} " +
            "</if> " +
            "<if test=\"order.payStatus != null and order.payStatus==34\"> " +
            " and o.pay_status in(3,4) " +
            "</if> " +

            "<if test=\"order.printStatus != null and order.printStatus==1\"> " +
            " and o.order_no in(select order_no from his_prescription_order_print) " +
            "</if> " +

            "<if test=\"order.printStatus != null and order.printStatus==2\"> " +
            " and o.pay_status !=4  and o.order_no not in(select order_no from his_prescription_order_print) " +
            "</if> " +

            "<if test=\"order.isFlag != null and order.isFlag==1 \"> " +
            " and o.order_condition in(3,4,5) and o.pay_status in(1,3) " +
            "</if> " +
            "<if test=\"order.orderType != null and order.orderType !=''\"> " +
            " and o.order_type = #{order.orderType} " +
            "</if> " +

            "<if test=\"order.deliveryType != null and order.deliveryType !=''\"> " +
            " and o.delivery_type = #{order.deliveryType} " +
            "</if> " +

            "<choose>"+
            "<when test=\"order.orderCondition > 0 and order.orderCondition &lt; 4\"> " +
            " and o.order_condition = #{order.orderCondition} " +
            " and o.pay_status != 4 " +
            "</when> " +
            "<when test=\"order.orderCondition != null\"> " +
            " and o.order_condition = #{order.orderCondition} " +
            "</when> " +
            "</choose>"+

            "<if test=\"order.prescriptionType != null\"> " +
            " and info.prescription_type = #{order.prescriptionType} " +
            "</if> " +
            "<if test=\"order.patientName != null and order.patientName !=''\"> " +
            " and (o.patient_name LIKE CONCAT('%',#{order.patientName},'%') or a.phone=#{order.patientName}) " +
            "</if> " +
            "<if test=\"order.orderNos != null and order.orderNos.size() > 0\"> " +
            " and o.order_no in <foreach item=\"item\" index=\"index\" collection=\"order.orderNos\" open=\"(\" separator=\",\" close=\")\"> #{item} </foreach>"+
            "</if> " +
            "<if test=\"order.orderNo != null and order.orderNo !=''\"> " +
            " and (o.order_no=#{order.orderNo} or o.api_order_no=#{order.orderNo}) " +
            "</if> " +
            "<if test=\"order.docName != null and order.docName!=''\"> " +
            " and o.doc_name LIKE CONCAT('%',#{order.docName},'%') " +
            "</if> " +
            "<if test=\"order.orderTime != null and order.orderTime.size() > 0 \">\n" +
            "and o.create_time  &gt; #{order.orderTime[0]}\n" +
            "and o.create_time  &lt;  str_to_date(DATE_ADD(#{order.orderTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>" +
            "<if test=\"order.payTime != null and order.payTime.size() > 0 \">\n" +
            "and o.pay_time  &gt; #{order.payTime[0]}\n" +
            "and o.pay_time  &lt; str_to_date(DATE_ADD(#{order.payTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "</if>"+
            "<if test=\"order.adjustTime != null and order.adjustTime.size() > 0 \">\n" +
            "and ((o.adjust_time  <![CDATA[ >= ]]> #{order.adjustTime[0]}\n" +
            "and o.adjust_time  &lt; str_to_date(DATE_ADD(#{order.adjustTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')) or (" +
            "o.next_time  <![CDATA[ >= ]]> #{order.adjustTime[0]}\n" +
            "and o.next_time  &lt; str_to_date(DATE_ADD(#{order.adjustTime[1]},INTERVAL 1 DAY), '%Y-%m-%d %H')\n" +
            "))\n" +
            "</if>"+
            "order by o.next_time desc,o.pay_time desc, o.create_time desc" +
            "\n</script>")
    List<LocalOrderDto> findAllByPageableForYun(@Param("order") PrescriptionQueryCriteria criteria);


    /**
     * @Author yzq
     * @Description 根据订单好，查找订单附加费用总额
     * @Date 2021/9/9 14:24
     * @Param [orderNo]
     * @return com.zgjkhis.modules.charges.service.dto.OrderChargeDto
     **/

    @Select("<script>select order_no,IFNULL(sum(t.total_price*t.quantity),0) as totalFee from his_prescription_charge t where t.order_no=#{orderNo}</script>")
    OrderChargeDto queryOrderChargeFee(@Param("orderNo") String orderNo);

    /**
     * 待退费数量
     * @param pharmacyId
     * @param prescriptionType
     * @return
     */
    @Select("<script>select\n" +
            "\tcount( 1 ) \n" +
            "from\n" +
            "\t(\n" +
            "\tselect\n" +
            "\t\torder_no\n" +
            "\tfrom\n" +
            "\t\this_prescription_order o \n" +
            "\twhere\n" +
            "\t\to.order_type = 2 \n" +
            "\t\tand o.pharmacy_id = #{pharmacyId} \n" +
            "\t\tand o.refund_flag = 1\n" +
            "\t\tand o.pay_status in ( 3, 4 )\n" +
            "\t) oo\n" +
            "\tinner join his_prescription_info info on oo.order_no = info.order_no \n" +
            "\tand info.prescription_type = #{type} </script>")
    int waitRefund(@Param("pharmacyId") String pharmacyId, @Param("type")  Integer prescriptionType);

    /**
     * 查询待审方数
     * @param pharmacyId
     * @param prescriptionType
     * @return
     */
    @Select("<script>select count(1) from ( select * from  his_prescription_order o where o.order_type = 2 and o.order_condition = 1 \n" +
            "and o.pharmacy_id = #{pharmacyId} and o.pay_status in (1,3)) oo inner join his_prescription_info info on oo.order_no = info.order_no \n" +
            "<if test=\"type != null\"> " +
            " and info.prescription_type = #{type}" +
            "</if></script>")
    int waitCheck(@Param("pharmacyId") String pharmacyId, @Param("type")  Integer prescriptionType);

    /**
     * 根据物流业务单号更改订单状态
     * @param bspOrderNo
     */
    @Update("update his_prescription_order t set t.order_condition=3 where t.order_condition!=5 and t.order_no in(select order_no from his_logistics where bsp_order_no =#{bspOrderNo})")
    void updateOrderCondition(@Param("bspOrderNo")String bspOrderNo);

    /**
     * 根据物流业务单号查询订单
     * @param bspOrderNo
     * @return
     */
    @Select("select * from his_prescription_order t where t.order_no in(select order_no from his_logistics where bsp_order_no =#{bspOrderNo})")
    List<PrescriptionOrder> queryListByBspOrderNo(@Param("bspOrderNo")String bspOrderNo);

    @Select("<script>select count(1) from ( select * from  his_prescription_order o where \n" +
            " o.pharmacy_id = #{pharmacyId}  and o.pay_status !=4 and o.order_type=2 and order_no not in(select order_no from his_prescription_order_print)) oo inner join his_prescription_info info on oo.order_no = info.order_no \n" +
            "<if test=\"type != null\"> " +
            " and info.prescription_type = #{type}" +
            "</if></script>")
    int waitPrint(String pharmacyId, @Param("type")Integer prescriptionType);
}
